package com.haiheng.voiceandbook.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.haiheng.voiceandbook.Constants;
import com.haiheng.voiceandbook.R;
import com.haiheng.voiceandbook.commo.DownloadAPKCallBack;
import com.haiheng.voiceandbook.commo.VersionManager;
import com.haiheng.voiceandbook.view.LoadingDialog;

import androidx.annotation.NonNull;

/**
 * @Des:
 * @Author: hwz
 * @Date:2021/12/24 0024
 * @Version: 1.0
 */
public class UpdateDialog extends BaseDialog{
    Button negtive,positive;
    private String url;
    private String md5;
    private String fileLength;
    private boolean isForceUpdate;
    private Context context;
    public UpdateDialog(Context context,String url,String md5,String fileLength,Boolean isForceUpdate) {
        super(context, R.style.dialog_with_slide_anim);
        this.context = context;
        this.url = url;
        this.md5 = md5;
        this.fileLength = md5;
        this.isForceUpdate = isForceUpdate;
        initData();
        setGravity(Gravity.CENTER);
        setCanceledOnTouchOutside(isForceUpdate ? false : true);
    }

    private void initData() {
        setContentView(R.layout.dialog_download_apk);
        TextView tvContent = findViewById(R.id.message);
        negtive = findViewById(R.id.negtive);
        positive = findViewById(R.id.positive);
        tvContent.setText("是否立即更新程序");
        negtive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        positive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(url)){
                    LoadingDialog loadingDialog = new LoadingDialog(context);
                //点击空白处Dialog不消失
                    if (isForceUpdate){
                        loadingDialog.setCanceledOnTouchOutside(false);
                    }else{
                        loadingDialog.setCanceledOnTouchOutside(true);
                    }
                    loadingDialog.showDialog();
                    //下载APK
                    VersionManager.Companion.downloadAPByRange(Constants.APK_NAME, url, md5,fileLength,new DownloadAPKCallBack() {
                        @Override
                        public void onResult(@NonNull String data) {
                            VersionManager.Companion.installNormal(context,data);
                            loadingDialog.dismiss();
                        }

                        @Override
                        public void onError(@NonNull String data) {
                            Toast.makeText(context,"下载错误："+data,Toast.LENGTH_SHORT).show();
                            loadingDialog.dismiss();
                        }

                        @Override
                        public void onLoading(@NonNull String data, int prosse) {

                        }
                    });
                } else {
                    Toast.makeText(context,"下载地址为空...",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
